package com.asher.hmone.slice;

import com.google.zxing.client.ohos.component.FinderComponent;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.bundle.IBundleManager;

public class MainAbilitySlice extends AbilitySlice {

    private DependentLayout myLayout = new DependentLayout(this);
    private Context mContext= null;
    private String[] permission = new String[]{"ohos.permission.CAMERA"};
    private String cameraPermission = "ohos.permission.CAMERA";
    private static int CAMERAPERMISSION = 10;
    private FinderComponent finderComponent;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);
        mContext = this;

        Text text = new Text(this);
        text.setLayoutConfig(config);
        text.setText("点击跳转扫描页面");
        text.setTextColor(new Color(0xFF000000));
        text.setTextSize(50);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isCameraAllow()){
                    Intent intent1 = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withBundleName("com.asher.hmone")
                            .withAbilityName("com.asher.hmone.ScanAbility")
//                        .withAbilityName("com.asher.zxingmodule.ScanAbility")
                            .build();
                    intent1.setOperation(operation);
                    startAbility(intent1);
                }else{
                    checkCameraPermission();
                }
            }
        });

        finderComponent = new FinderComponent(this);
        finderComponent.setLayoutConfig(config);
        myLayout.addComponent(text);
        myLayout.addComponent(finderComponent);
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
        if (finderComponent != null){
            finderComponent.animStart();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (finderComponent != null){
            finderComponent.stopAnim();
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    //检查权限
    private void checkCameraPermission() {

        if (!isCameraAllow()) {
                // 调用者无权限，做错误处理
                new ToastDialog(mContext).setContentText("no Camera permission").show();
            if (!canRequestPermission(cameraPermission)){
                new ToastDialog(mContext).setContentText("can not request Camera permission").show();
            }else{
                requestPermissionsFromUser(permission,CAMERAPERMISSION);
            }
            return;
        }
    }

    //判断是否已经获取相机权限
    private boolean isCameraAllow(){
        return verifyCallingPermission(cameraPermission) == IBundleManager.PERMISSION_GRANTED;
    }

    //权限设置回调
//    @Override
//    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
//        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
//    }

}
